# https://g1879.gitee.io/drissionpagedocs/WebPage/intro
import json
import queue
import threading
from time import sleep
from DrissionPage import WebPage


from DrissionPage import ChromiumOptions

import os 
os.environ["DISPLAY"] = "localhost:10"

print(os.environ)
# 设置参数 https://g1879.gitee.io/drissionpagedocs/ChromiumPage/create_page_obj/
path = "/usr/bin/google-chrome-stable"

# https://g1879.gitee.io/drissionpagedocs/QandA
co = (
    ChromiumOptions(ini_path="./dp_configs.ini")
    .set_browser_path(path)
    # .set_argument("--no-sandbox --hide-crash-restore-bubble")
    .set_argument("--no-sandbox")
    .set_pref("credentials_enable_service", False)
 
)
# 默认d模式创建对象
page = WebPage(chromium_options=co)

# 模式
print(page.mode)


# 跟监听器配合，可在获取到需要的数据包时，主动停止加载。
page.set.load_mode.none()  # 设置加载模式为none


page.get("https://club.magedu.com/")  # 访问网站

def login():
    button = page.ele("立即登录")

    if button:
        page.stop_loading()  # 主动停止加载
        button.click('js')
        

        # 手工登陆
        # 进入未答疑区
        # https://g1879.gitee.io/drissionpagedocs/get_elements/simplify
        button = page.ele("tx:授权登录")
        button.click('js')


login()

# 待解决
button = page.ele("tx:待解决")
page.stop_loading() 

result_queue = queue.Queue()

def get_row(body):
    # 从接口响应中取每行
    yield from  packet.response.body['data']['list']

threads = []

page.listen.start('v1/api/community/question/selectQuestionsByPage')  # 指定监听目标并启动监听
button.click('js')

i = 0 
for packet in page.listen.steps():
    # print(packet.response.body)  # 打印数据包url
    result_queue.put(get_row(packet.response.body))

    sleep(2)
    page.scroll.to_bottom()
    i += 1
    if i == 2:
        break

# 获取所有线程的返回值
while not result_queue.empty():
    for i in result_queue.get():
        print(i)